package  
{
	import flash.display.FocusDirection;
	import flash.events.MouseEvent;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.system.*;
	import flash.filesystem.File;
	import flash.filesystem.FileStream;
	import flash.filesystem.FileMode;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author patsup
	 * 
	 * 		This class handles the functionality for the menu prior to the start of every round
	 * 	(before the "PGame" begins/continues).
	 * 		The buttons are of the generalized class MenuButton.
	 * 
	 * 		AchievBtnInputMouseClick() takes you to another "menu"
	 * 		ViewPayoutBtnInputMouseClick() just pops up the payout table as another "big button"
	 * 
	 * *** protected functions
	 * 
	 * PlayBtnInputMouseClick():void
	 * MenuBtnInputMouseClick():void
	 * IncBetBtnInputMouseClick():void
	 * DecBetBtnInputMouseClick():void
	 * AchievBtnInputMouseClick():void
	 * ViewPayoutBtnInputMouseClick():void
	 * PayoutDisplayInputMouseClick():void
	 * 
	 */
	public class PreRoundMenu extends Menu 
	{
		//preround menu image parameters
		private const COLOR:int = 0x8080FF;
		private const WIDTH:int = 800;
		private const HEIGHT:int = 480;
		//buttons
		//private const PLAY_BUTTON_X_STARTLOC:int = 500;			//Button for Play Round
		//private const PLAY_BUTTON_Y_STARTLOC:int = 400;
		//private const P_BTN_COLOR:int = 0xFF8000;
		//private const P_BTN_WIDTH:int = 120;
		//private const P_BTN_HEIGHT:int = 40;
		//private const MENU_BUTTON_X_STARTLOC:int = 750;			//Button for return Back to menu
		//private const MENU_BUTTON_Y_STARTLOC:int = 430;
		//private const M_BTN_COLOR:int = 0xC0C0C0;
		//private const M_BTN_WIDTH:int = 45;
		//private const M_BTN_HEIGHT:int = 60;
		//private const INCR_BET_BUTTON_X_STARTLOC:int = 560;		//Button for Increase Bet
		//private const INCR_BET_BUTTON_Y_STARTLOC:int = 80;
		//private const I_BTN_COLOR:int = 0xFF8000;
		//private const I_BTN_WIDTH:int = 60;
		//private const I_BTN_HEIGHT:int = 40;
		//private const DECR_BET_BUTTON_X_STARTLOC:int = 400;		//Button for Decrease Bet
		//private const DECR_BET_BUTTON_Y_STARTLOC:int = 80;
		//private const D_BTN_COLOR:int = 0xFF8000;
		//private const D_BTN_WIDTH:int = 60;
		//private const D_BTN_HEIGHT:int = 40;
		private const ACHIEV_BUTTON_X_STARTLOC:int = 600;		//Button for Achievements menu
		private const ACHIEV_BUTTON_Y_STARTLOC:int = 130;
		private const PAYOUT_BTN_X:int = 340;					//Button for view payout
		private const PAYOUT_BTN_Y:int = 380;
		private const PO_BTN_COLOR:int = 0xFFFF80;
		private const PO_BTN_WIDTH:int = 60;
		private const PO_BTN_HEIGHT:int = 80;
		private const PAYOUT_DISP_X:int = 0;					//Payout Display
		private const PAYOUT_DISP_Y:int = 0;
		private const PO_DSP_COLOR:int = 0xFFFF00;
		private const PO_DSP_WIDTH:int = 800;
		private const PO_DSP_HEIGHT:int = 480;
		private const PO_DSP_ALPHA:Number = 0.8;
		//text fields
		//private const BET_TXT_X_STARTLOC:int = 500;					//Text for Current Bet Size
		//private const BET_TXT_Y_STARTLOC:int = 80;
		//private const MONEY_TXT_X_STARTLOC:int = 500;				//Text for Total Money
		//private const MONEY_TXT_Y_STARTLOC:int = 30;
		
		//button image data
		//private var playButtonImgData:ImageData;
		//private var menuButtonImgData:ImageData;
		//private var incrBetButtonImgData:ImageData;
		//private var decrBetButtonImgData:ImageData;
		private var achievButtonImgData:ImageData;
		private var viewPayoutButtonImgData:ImageData;
		private var payoutDisplayImgData:ImageData;
		
		//buttons
		//private var playButton:GenButton;
		//private var menuButton:GenButton;						//return to start menu button
		//private var incrBetButton:GenButton;
		//private var decrBetButton:GenButton;
		private var achievButton:AchievementButton;
		//private var viewPayoutButton:MenuButton;
		private var payoutDisplay:MenuButton;
		
		//TEST: data displaying
		//private var TEST_getDataButton:GenButton;				//for testing purposes, display data
		//private var TEST_dataDisplayImgData:ImageData;
		//private var TEST_dataDisplay:MenuButton;
		
		//text fields
		private var currentBetText:TextField;
		private var totalMoneyText:TextField;
		private var txtFormat:TextFormat;
		
		private var txtLabel:TextField;
		
		private var mainRef:Main;
		private var gameRef:PGame;
		
		private var PreRoundScreen:PreRoundMenuScreen;
		
		public function PreRoundMenu(mainRef:Main, gameRef:PGame) 
		{
			PreRoundScreen = new PreRoundMenuScreen();
			
			
			
			txtLabel = new TextField();
			this.mainRef = mainRef;
			this.gameRef = gameRef;
			gameRef.sesStats.visible = false;
			
			//background
			//the background
			var _bgImg:Med_Scroll_BG = new Med_Scroll_BG();
			_bgImg.width = 800;
			_bgImg.height = 480;
			addChild(_bgImg);
			graphics.beginFill(COLOR);
			graphics.drawRect(0, 0, WIDTH, HEIGHT);
			graphics.endFill();
			
			txtLabel.text = "Pre Round Menu";
			addChild(txtLabel);
			
			//button image data
			//playButtonImgData = new ImageData(PLAY_BUTTON_X_STARTLOC, PLAY_BUTTON_Y_STARTLOC, "Play");
			//menuButtonImgData = new ImageData(MENU_BUTTON_X_STARTLOC, MENU_BUTTON_Y_STARTLOC, "Back", M_BTN_COLOR, M_BTN_WIDTH, M_BTN_HEIGHT);
			//incrBetButtonImgData = new ImageData(INCR_BET_BUTTON_X_STARTLOC, INCR_BET_BUTTON_Y_STARTLOC, "+ + +", I_BTN_COLOR, I_BTN_WIDTH, I_BTN_HEIGHT, 1, "Ellipse");
			//decrBetButtonImgData = new ImageData(DECR_BET_BUTTON_X_STARTLOC, DECR_BET_BUTTON_Y_STARTLOC, "- - -", D_BTN_COLOR, D_BTN_WIDTH, D_BTN_HEIGHT, 1, "Ellipse");
			viewPayoutButtonImgData = new ImageData(PAYOUT_BTN_X, PAYOUT_BTN_Y, "View\nPayouts", PO_BTN_COLOR, PO_BTN_WIDTH, PO_BTN_HEIGHT, 1, "Ellipse");
			var _payoutTable:String = gameRef.GetSS().GetPayoutTable();
			payoutDisplayImgData = new ImageData(PAYOUT_DISP_X, PAYOUT_DISP_Y, _payoutTable, PO_DSP_COLOR, PO_DSP_WIDTH, PO_DSP_HEIGHT, PO_DSP_ALPHA);
			
			//buttons
			var _txtf:TextFormat = new TextFormat();
			_txtf.color = 0xD9E043;
			_txtf.size = 50;
			_txtf.bold = true;
			
			
			//playButton = new GenButton(PLAY_BUTTON_X_STARTLOC, PLAY_BUTTON_Y_STARTLOC, ScrollButton, PlayBtnInputMouseClick, "Play");
			PreRoundScreen.play_button.addEventListener(MouseEvent.CLICK, PlayBtnInputMouseClick);
			
			////menuButton = new MenuButton(menuButtonImgData, MenuBtnInputMouseClick);
			//menuButton = new GenButton(MENU_BUTTON_X_STARTLOC, MENU_BUTTON_Y_STARTLOC, Round_Button, MenuBtnInputMouseClick);
			PreRoundScreen.main_button.addEventListener(MouseEvent.CLICK, MenuBtnInputMouseClick);
			
			//incrBetButton = new GenButton(INCR_BET_BUTTON_X_STARTLOC, INCR_BET_BUTTON_Y_STARTLOC, Round_Button, IncBetBtnInputMouseClick, ">", _txtf);
			PreRoundScreen.inc_bet.addEventListener(MouseEvent.CLICK, IncBetBtnInputMouseClick);
			
			//decrBetButton = new GenButton(DECR_BET_BUTTON_X_STARTLOC, DECR_BET_BUTTON_Y_STARTLOC, Round_Button, DecBetBtnInputMouseClick, "<", _txtf);
			PreRoundScreen.dec_bet.addEventListener(MouseEvent.CLICK, DecBetBtnInputMouseClick);
			
			achievButton = new AchievementButton(mainRef, ACHIEV_BUTTON_X_STARTLOC, ACHIEV_BUTTON_Y_STARTLOC, AchievBtnInputMouseClick);
			PreRoundScreen.ach_tab.addEventListener(MouseEvent.CLICK, AchievBtnInputMouseClick);
			
			//viewPayoutButton = new MenuButton(viewPayoutButtonImgData, ViewPayoutBtnInputMouseClick);
			PreRoundScreen.payout_tab.addEventListener(MouseEvent.CLICK, ViewPayoutBtnInputMouseClick);
			
			
			payoutDisplay = new MenuButton(payoutDisplayImgData, PayoutDisplayInputMouseClick);
			
			//test data
			//TEST_dataDisplayImgData = new ImageData(PAYOUT_DISP_X, PAYOUT_DISP_Y, "Achievement data has been copied to clipboard!", PO_DSP_COLOR, PO_DSP_WIDTH, PO_DSP_HEIGHT, PO_DSP_ALPHA);
			//TEST_dataDisplay = new MenuButton(TEST_dataDisplayImgData, TEST_DataDisplayInputMouseClick);
			//TEST_getDataButton = new GenButton(10, 10, DiceTable, TEST_ViewDataBtnInputMouseClick, "    Copy Data");
			//addChild(TEST_getDataButton);
			
			//text fields
			var myFont:Font = new GLECB();
			txtFormat = new TextFormat();
			txtFormat.size = 55;
			txtFormat.font = myFont.fontName;
			//currentBetText = new TextField();
			//currentBetText.x = BET_TXT_X_STARTLOC;
			//currentBetText.y = BET_TXT_Y_STARTLOC;
			//currentBetText.width = 50;
			//currentBetText.height = 50;
			//totalMoneyText = new TextField();
			//totalMoneyText.x = MONEY_TXT_X_STARTLOC;
			//totalMoneyText.y = MONEY_TXT_Y_STARTLOC;
			//totalMoneyText.width = 50;
			//totalMoneyText.height = 50;
			
			/*addChild(playButton);
			addChild(menuButton);
			addChild(incrBetButton);
			addChild(decrBetButton);
			
			addChild(viewPayoutButton);
			addChild(currentBetText);
			addChild(totalMoneyText);*/
			addChild(achievButton);
			addChild(PreRoundScreen);
			
			PreRoundScreen.round_txt.text = gameRef.GetSS().GetNumRounds().toString();
			//PreRoundScreen.round_txt.setTextFormat(txtFormat);
			
			PreRoundScreen["ach_1"].addEventListener(MouseEvent.CLICK, AchievBtnInputMouseClick);
			PreRoundScreen["ach_2"].addEventListener(MouseEvent.CLICK, AchievBtnInputMouseClick);
			PreRoundScreen["ach_3"].addEventListener(MouseEvent.CLICK, AchievBtnInputMouseClick);
			
			Refresh();
			InitImage();
			
		}
		
		//Refresh()
		//call this to refresh the data
		public function Refresh():void
		{
			PreRoundScreen.coin_txt.text = "= $" + gameRef.GetSS().GetTotalMoney().toString();
			//PreRoundScreen.coin_txt.setTextFormat(txtFormat);
			
			PreRoundScreen.bet_txt.text = "= $" + gameRef.GetSS().GetCurrentBetSize().toString();
			//PreRoundScreen.bet_txt.setTextFormat(txtFormat);
			
			PreRoundScreen.round_txt.text = gameRef.GetSS().GetNumRounds().toString();
			//PreRoundScreen.round_txt.setTextFormat(txtFormat);
			//currentBetText.text = gameRef.GetSS().GetCurrentBetSize().toString();
			//totalMoneyText.text = "$" + gameRef.GetSS().GetTotalMoney().toString();
			//currentBetText.setTextFormat(txtFormat);
			//totalMoneyText.setTextFormat(txtFormat);
			achievButton.Refresh(PreRoundScreen);
			
			if (achievButton.achievementsReceived.length > 0) {
				var _dispAchIdx:int = 0;
				
				PreRoundScreen["ach_1"].visible = true;
				PreRoundScreen["ach_2"].visible = true;
				PreRoundScreen["ach_3"].visible = true;
				
				for (var i:int = achievButton.achievementsReceived.length; i > achievButton.achievementsReceived.length-3 && i > 0; i--) {
					var _currentAch:int = achievButton.achievements.achievementList.indexOf(achievButton.achievementsReceived[i-1]);
					PreRoundScreen["ach_" + (_dispAchIdx + 1)].gotoAndStop(_currentAch + 1);
					_dispAchIdx++;
				}
			}
			else {
				PreRoundScreen["ach_1"].visible = false;
				PreRoundScreen["ach_2"].visible = false;
				PreRoundScreen["ach_3"].visible = false;
			}
			
			//dont display acheivement icons if you havent gotten all three yet
			if (achievButton.achievementsReceived.length == 2) {
				PreRoundScreen["ach_3"].visible = false;	
			}
			if (achievButton.achievementsReceived.length == 1) {
				PreRoundScreen["ach_2"].visible = false;
				PreRoundScreen["ach_3"].visible = false;	
			}
			
			PreRoundScreen.bust.gotoAndStop(mainRef.playerIndex);
		}
		
		//InitImage()
		//Initializes the image of the Menu
		protected override function InitImage():void
		{
			super.InitImage();
		}
		
		//PlayBtnInputMouseClick()
		//called on play button mouse click
		protected function PlayBtnInputMouseClick(e:MouseEvent):void
		{
			mainRef.PlayRound(this);
		}
		//MenuBtnInputMouseClick()
		//called on back menu button mouse click
		protected function MenuBtnInputMouseClick(e:MouseEvent):void
		{
			mainRef.ReturnToStartMenu(this);
		}
		//IncBetBtnInputMouseClick()
		//called on increase bet menu button mouse click
		protected function IncBetBtnInputMouseClick(e:MouseEvent):void
		{
			//if(mainRef.achievMenu)
			for (var i:int = 0; i < mainRef.achievMenu.achievements.achRcvdIdx.length; i++){
				trace(mainRef.achievMenu.achievements.achRcvdIdx[i]);
			}
			gameRef.IncreaseBetSize();
			gameRef.RefreshSessionText();
			PreRoundScreen.bet_txt.text = "= $" + gameRef.GetSS().GetCurrentBetSize().toString();
			//currentBetText.text = gameRef.GetSS().GetCurrentBetSize().toString();
			//currentBetText.setTextFormat(txtFormat);
		}
		//DecBetBtnInputMouseClick()
		//called on decrease bet button mouse click
		protected function DecBetBtnInputMouseClick(e:MouseEvent):void
		{
			gameRef.DecreaseBetSize();
			gameRef.RefreshSessionText();
			PreRoundScreen.bet_txt.text = "= $" + gameRef.GetSS().GetCurrentBetSize().toString();
			//currentBetText.text = gameRef.GetSS().GetCurrentBetSize().toString();
			//currentBetText.setTextFormat(txtFormat);
		}
		//AchievBtnInputMouseClick()
		//called on back menu button mouse click
		protected function AchievBtnInputMouseClick(e:MouseEvent):void
		{
			mainRef.DisplayAchievMenu(this);
		}
		//ViewPayoutBtnInputMouseClick()
		//mouseclick will pop up payout display
		protected function ViewPayoutBtnInputMouseClick(e:MouseEvent):void
		{
			addChild(payoutDisplay);
		}
		//PayoutDisplayInputMouseClick()
		//mouseclick will remove the payout display
		protected function PayoutDisplayInputMouseClick():void
		{
			removeChild(payoutDisplay);
		}
		
		//private function saveTrackData(event:Event):void 
		//{
			//var newFile:File = event.target as File;
			//var str:String = "Hello.";
			//if (!newFile.exists)
			//{
				//var stream:FileStream = new FileStream();
				//stream.open(newFile, FileMode.WRITE);
				//stream.writeUTFBytes(str);
				//stream.close();
			//}
		//}			
		protected function TEST_ViewDataBtnInputMouseClick():void
		{
			//var docsDir:File = File.documentsDirectory;
			//try
			//{
				//docsDir.browseForSave("Save As");
				//docsDir.addEventListener(Event.SELECT, saveTrackData);
			//}
			//catch (error:Error)
			//{
				//trace("Failed:", error.message);
			//}
			
			
			//System.setClipboard(mainRef.GetGameRef().GetSS().GetFullTrackedDataString());
			//addChild(TEST_dataDisplay);
		}
		protected function TEST_DataDisplayInputMouseClick():void
		{
			//removeChild(TEST_dataDisplay);
		}		
	}

}